<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML>
<html style="height:450px;">
<head>
<base href="<%=basePath%>">

<title>增加建筑数量</title>
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<link rel="stylesheet" href="js/bootstrap2/css/bootstrap.min.css">
<link rel="stylesheet" href="css/common.css">
<link href="js/select2/select2.min.css" type="text/css" rel="stylesheet" />

<script src="js/jquery-1.11.1.js"></script>
<script src="js/bootstrap2/js/bootstrap.min.js"></script>
<script src="js/select2/select2.min.js"></script>

<script src="js/layui-v2.1.5/layui/layui.all.js"></script>
<script src="js/form.js"></script>

<style type="text/css">
form {
	margin-top: 10px;
}

.gap {
	display: inline-block;
	width: 10px;
}
</style>
<script type="text/javascript">
	var flag;
	$(function() {
		var s2 = $("#buildingStructure").select2({
			 tags: true
		});
		var s3 = $("#towerInterval").select2({
			 tags: true
		});
		var s4 = $("#towerDirection").select2({
			 tags: true
		});
		s2.val('${bNum.buildingStructure}').trigger("change");
		$("input[name='buildingName']").focus();
		//如果已经保存过了，则开启只读状态
		<c:if test="${not empty requestScope.bNum }">
			$("#buildingNumForm").find("input,textarea").attr("readonly",true);
			$("#buildingNumForm").find("select").attr("disabled",true);
		</c:if>
		//如果numFlag 1 查看页面，隐藏编辑按钮
		<c:if test="${numFlag==1 }">
			$("#btns-opt").hide();
		</c:if>
		mySubmit("buildingNumForm", initBuildingNumData, buildingNumsuccess);
		
	});
	
	var optBuildingNum;
	//optBuildingNum操作字段，0：临时保存；1：新增；2：修改
	function saveBuildingNum(val){
		optBuildingNum=val;
		if(val==3){
			//解除锁定状态
			var index=layer.confirm('确定开启编辑状态？', {
			 	offset:'20%',
				shade:0
			}, function(){
			  //去除只读开启按钮编辑
			   $("#buildingNumForm").find("input,textarea").attr("readonly",false);
			   $("#buildingNumForm").find("select").attr("disabled",false);
			   $("#buildingNumForm #modifyBtn").hide();
			  $("#buildingNumForm .hidbtn").addClass("showbtn");
			   layer.close(index);
			});
		}else{
			$("#buildingNumForm").submit();
		}
	}
	
	function onBlur2(){
		var fkProjectid = parent.parent.parent.projectID; //建筑id
		var buildingName = $("#buildingName").val();
		var oldName = '${bNum.buildingName}'
		$.post("buildingNumber/getBuildingNumBybuildingName",{fkProjectid:fkProjectid,buildingName:buildingName},function(data,status){
			if(data.length==0){
				if(!valLength(20,buildingName)){
					$("#validate_msg").text("建筑名称长度不能超过20");
					flag = false;
				}else{
					$("#validate_msg").text("");
	        	flag = true;
				}
	        	
			}else{
				if(oldName==buildingName){
					$("#validate_msg").text("");
		        	flag = true;
				}else{
					$("#validate_msg").text("该建筑已存在");
					flag = false;
				}
			}
		},"json");
	}
	
	function initBuildingNumData() {
		var i = layer.load(2);
		if(flag==true){
			var id = $("input[name='id']").val();
			var buildingName = $("input[name='buildingName']").val();
			var buildingStructure = $("select[name='buildingStructure']").val();
			var fireProofingLv = $("input[name='fireProofingLv']").val();
			var antiKnockLv = $("input[name='antiKnockLv']").val();
			var towerInterval = $("select[name='towerInterval']").val();
			var towerDirection = $("select[name='towerDirection']").val();
			var towerName = $("input[name='towerName']").val();
			var fkProjectid = parent.parent.parent.projectID; //建筑id
			if(towerInterval==""){
				towerInterval=0.0;
			}
			if(!valLength(20,buildingName)){
				layer.msg("建筑名称长度不能超过20");
				$("input[name='buildingName']").focus();
				layer.close(i);
				return null;
			}else if (!testNum(towerInterval)) {
				layer.msg("楼宇间距请输入数值");
				$("input[name='towerInterval']").focus();
				layer.close(i);
				return null;
			} else {
				return {
					id:id,
					towerDirection:towerDirection,
					towerName:towerName,
					buildingName : buildingName,
					buildingStructure : buildingStructure,
					fireProofingLv : fireProofingLv,
					antiKnockLv : antiKnockLv,
					towerInterval : towerInterval,
					fkProjectid : fkProjectid,
					opt : optBuildingNum
				};
			}
		}else{
		layer.close(i);
			return null;
		}
	}

	function buildingNumsuccess(data, status) {
		if (data.data == "success") {
			layer.alert("添加成功!", function(i) {
				parent.getBuildingNum();
				bnClose();
			});
		} else {
			layer.alert("未知错误！");
			layer.closeAll();
		}
	}

	//关闭当前窗体
	function bnClose() {
		parent.layer.closeAll();
	}
	
	
</script>
</head>

<body >
	<form class="form-horizontal" id="buildingNumForm" method="post"
		action="buildingNumber/addBuildingNum" onsubmit="return false">
		<div class="control-group">
			<label class="control-label" for="inputEmail">建筑名称</label>
			<div class="controls">
				<input type="hidden" name="id" id="entityId_id" value="${bNum.id}"/>
				<input type="text" name="buildingName" id="buildingName" placeholder="" onblur="onBlur2()" value="${bNum.buildingName}"/>
				<span id="validate_msg" style="color:red"></span>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label" for="inputPassword">建筑结构</label>
			<div class="controls">
				<select  class="js-example-tags" name="buildingStructure" id="buildingStructure" style="width:175px;">
					<option value="" >请选择</option>
					<!-- 
					<option value="钢结构"  >钢结构</option>
					<option value="钢混结构">钢混结构</option>
					<option value="砌体结构">砌体结构</option> -->
					
					<c:forEach var="t" items="${requestScope.list3 }" >
						<c:choose>
							<c:when test="${pageScope.t eq requestScope.bNum.buildingStructure }">
								<option value="${pageScope.t}" selected="selected" >${pageScope.t}</option>
							</c:when>
							<c:otherwise>
								<option value="${pageScope.t}" >${pageScope.t}</option>
							</c:otherwise>
						</c:choose>
					</c:forEach>
					
				</select>
			</div> 
			
		</div>

		<div class="control-group">
			<label class="control-label" for="inputPassword">防火等级</label>
			<div class="controls">
				<input type="text" name="fireProofingLv" placeholder="" value="${bNum.fireProofingLv}"/>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label" for="inputPassword">抗震等级</label>
			<div class="controls">
				<input type="text" name="antiKnockLv" placeholder="" value="${bNum.antiKnockLv}"/>
			</div>
		</div>
		<!-- 楼宇间距 -->
		<div class="control-group">
			<label class="style_btn" for="inputPassword" style="margin-left:50px;">楼宇间距</label>
		</div>
		<div class="control-group">
			<label class="control-label" for="inputPassword">间距对象</label>
			<div class="controls">
				<input type="text" name="towerName" placeholder="" value="${bNum.towerName}"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="inputPassword">间距方向</label>
			<div class="controls">
				<select  class="js-example-tags" name="towerDirection" id="towerDirection" style="width:175px;">
					<option value="">请输入</option>
					<c:forEach var="l" items="${list2}" varStatus="name" >
						<c:if test="${l.towerDirection==bNum.towerDirection }">
							<option value="${l.towerDirection }" selected="selected">${l.towerDirection }</option>
						</c:if>
						<c:if test="${l.towerDirection!=bNum.towerDirection and !empty l.towerDirection }">
							<option value="${l.towerDirection }" >${l.towerDirection }</option>
						</c:if>
					</c:forEach>
				</select>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="inputPassword" >间距距离</label>
			<div class="controls">
				<select  class="js-example-tags" name="towerInterval" id="towerInterval" style="width:175px;">
					<option value="">请输入</option>
					<c:forEach var="l" items="${list}" varStatus="name" >
						<c:if test="${l.towerInterval==bNum.towerInterval }">
							<option value="${l.towerInterval }" selected="selected"><fmt:formatNumber value="${l.towerInterval }" pattern="##0.00"/></option>
						</c:if>
						<c:if test="${l.towerInterval!=bNum.towerInterval }">
							<option value="${l.towerInterval }" ><fmt:formatNumber value="${l.towerInterval }" pattern="##0.00"/></option>
						</c:if>
					</c:forEach>
				</select>m
			</div>
		</div>
		
		
		<div id="btns-opt" class="control-group" style="width:100%;text-align:center; padding-top:20px">
			<!-- <div class="controls">
				<button type="submit" class="btn btn-danger">保存</button>
				<span class="gap"></span>
				<button type="button" onclick="bnClose();" class="btn">取消</button>
			</div> -->
			<c:choose>
				<c:when test="${empty requestScope.bNum}">
					<button class="btn btn-danger radius my-btn" type="button"
						onclick="saveBuildingNum(0)" type="button">提交</button>
					<button class="btn btn-danger radius my-btn" type="button"
						onclick="saveBuildingNum(1)" type="button">保 存</button>
					<button type="button" class="btn btn-default radius"  onclick="resetForms('buildingNumForm')" >
						重置 </button>
					<br />
				</c:when>
				<c:otherwise>
					<button id="modifyBtn" class="btn btn-danger radius my-btn" type="button"
						onclick="saveBuildingNum(3)" type="button">启用编辑</button>
					<button class="btn btn-danger radius my-btn hidbtn" type="button"
						onclick="saveBuildingNum(2)" type="button">保存修改</button>
					<button type="button" class="btn btn-default radius hidbtn" onclick="resetForms('buildingNumForm')" >
						重置 </button>	
				</c:otherwise>
			</c:choose>
		</div>
	</form>
</body>
</html>
